window.onload = function(){
	let register = document.querySelector("button[class='register']");
	register.addEventListener("click",()=>{
		let username = document.querySelector(".username").value;
		console.log(username)
		let password = document.querySelector(".password").value;
		let again = document.querySelector(".again").value;
		const usernameReg = new RegExp(/^[a-zA-Z0-9_]{3,12}$/,'i');
		const passwordReg = new RegExp(/^[0-9]{6,12}$/,'i');
		if(!usernameReg.test(username)){
			alert("用户名由3-12位字母数字下划线组成！");
		}
		if(!passwordReg.test(password)){
			alert("密码由6-12位数字组成！");
		}
		if(password != again){
			alert("两次密码输入不一致！");
		}
		if(usernameReg.test(username) && passwordReg.test(password) && password == again){
			(function(){
				ajax({
					method:"POST",
					url:BASE_URL+"/api_user",
					params:"username="+username+"&password="+password+"&status=register",
					type:"JSON",
					callback:function(data){
						alert(data.message);
						window.location.href = 'login.html';
					}
				})
			})();
		}
	})
	let userNameInput = document.querySelector(".username");
	userNameInput.addEventListener("blur",()=>{
		let username = document.querySelector(".username").value;
		ajax({
			type:"JSON",
			method:"POST",
			url:BASE_URL+"/api_user",
			params:"username="+username+"&status=check",
			callback:function(data){
                alert(data.message);
			}
		})
	})
}


